package com.service.setting.impl;

import com.comm.R;
import com.entity.PrescriptionCost;
import com.github.pagehelper.PageHelper;
import com.mapper.setting.SysPrescriptionMapper;
import com.service.setting.SysPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SysPrescriptionServiceimpl implements SysPrescriptionService {
    @Autowired
    private SysPrescriptionMapper sysPrescriptionMapper;
    @Override
    public R fundPrescriptions(Integer currentPage,Integer pageSize) {
        R r = new R();

        try {
            com.github.pagehelper.Page<PrescriptionCost> page = PageHelper.startPage(currentPage,pageSize);
            //如果查询成功则，不会抛出异常则走这里
            List<PrescriptionCost> prescriptionCosts = sysPrescriptionMapper.fundPrescriptions();

            int fundcount = sysPrescriptionMapper.fundcount();
            r.setCode(200);
            r.setMsg("恭喜你查到了");
            r.setData(prescriptionCosts);
            r.setCount(fundcount);
            return r;

        }catch (Exception e){
            e.printStackTrace();
//            如果发生异常返回r对象
            r.setCode(500);
            r.setMsg("服务器开小差了");
            return r;
        }
    }

    @Override
    public R fundPrescription(String costname) {
        R r = new R();

        try {
            PrescriptionCost prescriptionCost = sysPrescriptionMapper.fundPrescription(costname);
            if (prescriptionCost != null){
                r.setCode(200);
                r.setMsg("恭喜你查到了");
                r.setData(prescriptionCost);
                return r;
            }else {
                r.setCode(200);
                r.setMsg("没有这个这项费用，请你添加！！！");
                r.setData(prescriptionCost);
                return r;
            }


        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了");
            return r;

        }
    }

    @Override
    public R addPrescriptions(PrescriptionCost prescriptionCost) {
        R r = new R();
        try {
            int i = sysPrescriptionMapper.addPrescriptions(prescriptionCost);
            if (i!=0){
                r.setCode(200);
                r.setMsg("恭喜你添加成功");
                return r;
            }else {

                r.setCode(520);
                r.setMsg("参数添加错误，检查好再进行添加");
                return r;
            }

        }catch (Exception e){
            e.printStackTrace();
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了");
            return r;
        }
    }

    @Override
    public R editPrescriptions(PrescriptionCost prescriptionCost) {
        R r = new R();

        try {
            int i = sysPrescriptionMapper.editPrescriptions(prescriptionCost);
            if (i>0){
                r.setCode(200);
                r.setMsg("编辑成功");
                return r;
            }else {
                r.setCode(520);
                r.setMsg("参数添加错误，检查好再进行添加");
                return r;
            }

        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了");
            return r;
        }
    }

    @Override
    public R delPrescriptions(Integer id) {
        R r = new R();
        try {
            int i = sysPrescriptionMapper.delPrescriptions(id);
            if (i>0){
                r.setCode(200);
                r.setMsg("删除成功");
                return r;
            }else {
                r.setCode(520);
                r.setMsg("发生了一个莫名其妙的错误，检查好再进行添加");
                return r;
            }

        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了");
            return r;
        }
    }
}
